package com.procurement.inquiry.service.impl;

import java.util.List;

import com.procurement.inquiry.domain.PcmApplicationDetail;
import com.procurement.inquiry.domain.PcmInquiryForm;
import com.procurement.inquiry.domain.PcmSupplier;
import com.ruoyi.system.api.domain.PcmAudit;
import com.ruoyi.system.api.domain.PcmSupplierQuotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.procurement.inquiry.domain.PcmInquiryDemand;
import com.procurement.inquiry.mapper.PcmInquiryFormMapper;
import com.procurement.inquiry.service.IPcmInquiryFormService;

/**
 * 询价单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-28
 */
@Service
public class PcmInquiryFormServiceImpl implements IPcmInquiryFormService 
{
    @Autowired
    private PcmInquiryFormMapper pcmInquiryFormMapper;

    /**
     * 查询询价单
     * 
     * @param inquiryId 询价单主键
     * @return 询价单
     */
    @Override
    public List<PcmSupplier> selectPcmInquiryFormByInquiryId(String inquiryId)
    {
        return pcmInquiryFormMapper.selectPcmInquiryFormByInquiryId(inquiryId);
    }

    /**
     * 查询询价单列表
     *
     * @return 询价单
     */
    @Override
    public List<PcmInquiryDemand> selectPcmInquiryFormList(String pcmInquiryDemand)
    {
        return pcmInquiryFormMapper.selectPcmInquiryFormList(pcmInquiryDemand);
    }

    /**
     * 新增询价单
     * 
     * @param pcmInquiryForm 询价单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPcmInquiryForm(PcmInquiryForm pcmInquiryForm)
    {
        int rows = pcmInquiryFormMapper.insertPcmInquiryForm(pcmInquiryForm);
        insertPcmInquiryDemand(pcmInquiryForm);
        return rows;
    }

    @Override
    public int insertPcmAudit(PcmAudit pcmAudit) {
        return pcmInquiryFormMapper.insertPcmAudit(pcmAudit);
    }

    /**
     * 修改询价单
     * 
     * @param pcmInquiryForm 询价单
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePcmInquiryForm(PcmInquiryForm pcmInquiryForm)
    {
        pcmInquiryFormMapper.deletePcmInquiryDemandByInquiryDemandNumber(pcmInquiryForm.getInquiryId());
        insertPcmInquiryDemand(pcmInquiryForm);
        return pcmInquiryFormMapper.updatePcmInquiryForm(pcmInquiryForm);
    }

    /**
     * 批量删除询价单
     * 
     * @param inquiryIds 需要删除的询价单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePcmInquiryFormByInquiryIds(Long[] inquiryIds)
    {
        pcmInquiryFormMapper.deletePcmInquiryDemandByInquiryDemandNumbers(inquiryIds);
        return pcmInquiryFormMapper.deletePcmInquiryFormByInquiryIds(inquiryIds);
    }

    /**
     * 删除询价单信息
     * 
     * @param inquiryId 询价单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePcmInquiryFormByInquiryId(Long inquiryId)
    {
        pcmInquiryFormMapper.deletePcmInquiryDemandByInquiryDemandNumber(inquiryId);
        return pcmInquiryFormMapper.deletePcmInquiryFormByInquiryId(inquiryId);
    }

    @Override
    public int insertPcmSupplierQuotation(PcmSupplierQuotation pcmSupplierQuotation) {
        return pcmInquiryFormMapper.insertPcmSupplierQuotation(pcmSupplierQuotation);
    }

    @Override
    public PcmSupplierQuotation selectSupplierQuotation() {
        return pcmInquiryFormMapper.selectSupplierQuotation();
    }

    @Override
    public int updatePcmApploicationDemand(List<PcmApplicationDetail> inquiryDemandNumber) {
        for (PcmApplicationDetail a: inquiryDemandNumber){
            pcmInquiryFormMapper.updatePcmApploicationDemand(String.valueOf(a.getDetailId()));
        }
        return 1;
    }

    /**
     * 新增询报价需求信息
     * 
     * @param pcmInquiryForm 询价单对象
     */
    public void insertPcmInquiryDemand(PcmInquiryForm pcmInquiryForm)
    {
        List<PcmInquiryDemand> pcmInquiryDemandList = pcmInquiryForm.getPcmInquiryDemandList();
        Long inquiryId = pcmInquiryForm.getInquiryId();
        if (StringUtils.isNotNull(pcmInquiryDemandList))
        {
            List<PcmInquiryDemand> list = new ArrayList<PcmInquiryDemand>();
            for (PcmInquiryDemand pcmInquiryDemand : pcmInquiryDemandList)
            {
                pcmInquiryDemand.setInquiryDemandNumber(String.valueOf(inquiryId));
                list.add(pcmInquiryDemand);
            }
            if (list.size() > 0)
            {
                pcmInquiryFormMapper.batchPcmInquiryDemand(list);
            }
        }
    }
}
